/**
 * 0. Project	: com.dnbns.bodywater 프로젝트
 *
 * 1. FileName	: LoginActivity.java
 * 2. Package	: com.dnbns.bodywater
 * 3. 작성자		: 윤화현
 * 4. 작성일		: 2013. 11. 15. 오전 10:49:15
 * 5. Comment	: 
 */

package com.dnbns.bodywater;

import java.util.Calendar;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.NumericWheelAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.ViewFlipper;

import com.dnbns.lib.RbPreference;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class LoginActivity extends Activity {

	private AdView mAdView;
	private ViewFlipper mViewFlipper;

	private int mPage1 = 0;
	private int mPage2 = 1;
	private int mPage3 = 2;
	private int mInPage = 0;
	
	private WheelView mYear;
	private WheelView mMonth;
	private WheelView mDay;
	private WheelView mWeight;
	
	private NumericWheelAdapter mYearList;
	private NumericWheelAdapter mMonthList;
	private NumericWheelAdapter mDayList;
	private NumericWheelAdapter mWeightList;
	
	private boolean mScrollWhell = false;
	
	private boolean isSexFlag = false;		//false : 남자, true : 여자.

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		mAdView = (AdView) this.findViewById(R.id.adView);
		AdRequest adRequest = new AdRequest.Builder().build();
		mAdView.loadAd(adRequest);
	
		init();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		getData();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		mAdView.resume();
	}

	@Override
	public void onPause() {
		mAdView.pause();
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		mAdView.destroy();
		super.onDestroy();
	}

/**
 *************************************************************** 
 * init
 *************************************************************** 
 */

	private void init() {
		mViewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);

		View login1 = getLayoutInflater().inflate(R.layout.activity_login1, null);
		View login2 = getLayoutInflater().inflate(R.layout.activity_login2, null);
		View login3 = getLayoutInflater().inflate(R.layout.activity_login3, null);

		mViewFlipper.addView(login1);
		mViewFlipper.addView(login2);
		mViewFlipper.addView(login3);

		ImageView next = (ImageView)findViewById(R.id.next);
		ImageView prev = (ImageView)findViewById(R.id.prev);
		
		next.setOnClickListener(OCL);
		prev.setOnClickListener(OCL);
		next.setOnTouchListener(OTL);
		prev.setOnTouchListener(OTL);

		initSex();
	}

	// 첫번째 화면
	private void initSex() {
		mInPage = mPage1;
		View view = mViewFlipper.getChildAt(mPage1);

		ImageView prev = (ImageView)findViewById(R.id.prev);
		prev.setVisibility(View.GONE);
		
		final ImageView men = (ImageView)view.findViewById(R.id.men);
		final ImageView women = (ImageView)view.findViewById(R.id.women);
		
		men.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				isSexFlag = false;
				if(isSexFlag){
					men.setBackgroundResource(R.drawable.start_1_man);
					women.setBackgroundResource(R.drawable.start_1_woman_on);
				}else{
					men.setBackgroundResource(R.drawable.start_1_man_on);
					women.setBackgroundResource(R.drawable.start_1_woman);
				}
			}
		});
		
		women.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				isSexFlag = true;
				if(isSexFlag){
					men.setBackgroundResource(R.drawable.start_1_man);
					women.setBackgroundResource(R.drawable.start_1_woman_on);
				}else{
					men.setBackgroundResource(R.drawable.start_1_man_on);
					women.setBackgroundResource(R.drawable.start_1_woman);
				}
			}
		});

		mViewFlipper.setInAnimation(null);
		mViewFlipper.setOutAnimation(null);
		mViewFlipper.setDisplayedChild(mPage1);
	}

	// 두번째 화면
	private void initBirth() {
		mInPage = mPage2;
		View view = mViewFlipper.getChildAt(mPage2);

		ImageView prev = (ImageView)findViewById(R.id.prev);
		prev.setVisibility(View.VISIBLE);

		mYear = (WheelView) view.findViewById(R.id.year);
		mMonth = (WheelView) view.findViewById(R.id.month);
		mDay = (WheelView) view.findViewById(R.id.day);

		// set current time
		Calendar c = Calendar.getInstance();
		int curYear		= c.get(Calendar.YEAR);
		
		mYearList = new NumericWheelAdapter(this, curYear-99, curYear);
		mMonthList = new NumericWheelAdapter(this, 1, 12);
		mDayList = new NumericWheelAdapter(this, 1, 31);
		
		mYear.setViewAdapter(mYearList);
		mMonth.setViewAdapter(mMonthList);
		mDay.setViewAdapter(mDayList);
		
		mYear.setCurrentItem(mYear.getViewAdapter().getItemsCount()-20);

		OnWheelChangedListener wheelListener = new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				if(!mScrollWhell){
				}
			}
		};

		OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
			public void onScrollingStarted(WheelView wheel) {
				mScrollWhell = true;
			}

			public void onScrollingFinished(WheelView wheel) {
				mScrollWhell = false;
			}
		};

		mYear.addChangingListener(wheelListener);
		mMonth.addChangingListener(wheelListener);
		mDay.addChangingListener(wheelListener);

		mYear.addScrollingListener(scrollListener);
		mMonth.addScrollingListener(scrollListener);
		mDay.addScrollingListener(scrollListener);

		mViewFlipper.setInAnimation(null);
		mViewFlipper.setOutAnimation(null);
		mViewFlipper.setDisplayedChild(mPage2);
	}
	
	// 세번째 화면
	private void initWeight() {
		mInPage = mPage3;
		View view = mViewFlipper.getChildAt(mPage3);

		ImageView prev = (ImageView)findViewById(R.id.prev);
		prev.setVisibility(View.VISIBLE);

		mWeight = (WheelView) view.findViewById(R.id.weight);
		
		mWeightList = new NumericWheelAdapter(this, 20, 300);
		
		mWeight.setViewAdapter(mWeightList);
		mWeight.setCurrentItem(60);

		OnWheelChangedListener wheelListener = new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				if(!mScrollWhell){
				}
			}
		};

		OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
			public void onScrollingStarted(WheelView wheel) {
				mScrollWhell = true;
			}

			public void onScrollingFinished(WheelView wheel) {
				mScrollWhell = false;
			}
		};

		mWeight.addChangingListener(wheelListener);
		mWeight.addScrollingListener(scrollListener);

		mViewFlipper.setInAnimation(null);
		mViewFlipper.setOutAnimation(null);
		mViewFlipper.setDisplayedChild(mPage3);
	}


/**
 *************************************************************** 
 * method
 *************************************************************** 
 */

	//이전데이터 가져오기.
	private void getData() {
		RbPreference pref = new RbPreference(getApplicationContext());
		
		if(pref.getValue("weight", null) != null){
			Intent mIntent = new Intent(getApplicationContext(), MainActivity.class);
			startActivity(mIntent);
			finish();
		}
	}
	
	//데이터 저장.
	private void setData() {
		RbPreference pref = new RbPreference(getApplicationContext());
		
		String birth = mYearList.getItemText(mYear.getCurrentItem()).toString();
		birth = birth + "-" + mMonthList.getItemText(mMonth.getCurrentItem()).toString();
		birth = birth + "-" + mDayList.getItemText(mDay.getCurrentItem()).toString();

		pref.put("weight", mWeightList.getItemText(mWeight.getCurrentItem()).toString());
		pref.put("birth", birth);
		pref.put("sex", isSexFlag);
		pref.put("alarm", false);
		pref.put("vibrate", false);
	}

	//이전
	private void prevPage(){
		Log.d("TEST", "PREV : "+mInPage);
		if(mInPage == mPage2){
			initSex();
		}else if(mInPage == mPage3){
			initBirth();
		}
	}
	
	//다음
	private void nextPage(){
		Log.d("TEST", "NEXT : "+mInPage);
		if(mInPage == mPage1){
			initBirth();
		}else if(mInPage == mPage2){
			initWeight();
		}else if(mInPage == mPage3){
			setData();
			Intent mIntent = new Intent(getApplicationContext(), LoginFinishActivity.class);
			startActivity(mIntent);
			finish();
		}
	}
	
/**
 *************************************************************** 
 * Event
 *************************************************************** 
 */

	View.OnClickListener OCL = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.prev:
				prevPage();
				break;
			case R.id.next:
				nextPage();
				break;
			default:
				break;
			}
		}
	};
	
	View.OnTouchListener OTL = new OnTouchListener() {
		
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.prev:
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					((ImageView)v).setBackgroundResource(R.drawable.start_left_on_btn);
				}else if(event.getAction() == MotionEvent.ACTION_UP){
					((ImageView)v).setBackgroundResource(R.drawable.start_left_btn);
				}
				break;
			case R.id.next:
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					((ImageView)v).setBackgroundResource(R.drawable.start_right_on_btn);
				}else if(event.getAction() == MotionEvent.ACTION_UP){
					((ImageView)v).setBackgroundResource(R.drawable.start_right_btn);
				}
				break;
			case R.id.finish:
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					((ImageView)v).setBackgroundResource(R.drawable.start_4_on_btn);
				}else if(event.getAction() == MotionEvent.ACTION_UP){
					((ImageView)v).setBackgroundResource(R.drawable.start_4_btn);
				}
				break;
			default:
				break;
			}
			return false;
		}
	};
}
